package com.example.oversea_home_spring.controller;


import com.example.oversea_home_spring.bean.Order;
import com.example.oversea_home_spring.bean.Rent;
import com.example.oversea_home_spring.bean.User;
import com.example.oversea_home_spring.bean.Warehouse;
import com.example.oversea_home_spring.service.OwnerService;
import com.example.oversea_home_spring.service.TraderService;
import com.example.oversea_home_spring.uitl.CodeMsg;
import com.example.oversea_home_spring.uitl.DateUtil;
import com.example.oversea_home_spring.uitl.Result;
import com.example.oversea_home_spring.uitl.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//商人
@RestController
@CrossOrigin//跨域
@RequestMapping("trader")
public class TraderController {

    @Autowired
    TraderService traderService;

    @Autowired
    OwnerService ownerService;

    //发布订单
    @PutMapping("addOrder")
    public Result<Integer> addOneOrder(Order order){
        order.setId(UUIDUtil.uuid());
        order.setCreate_time(DateUtil.myDate());
        order.setUpdate_time(DateUtil.myDate());
        order.setStatue((long)1);
        int isOk = traderService.addOneOrder(order);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.ADD_ORDER_ERROR);
    }

    //修改订单 status = 1
    @PutMapping("updateOrder")
    public Result<Integer> updateOneOrder(Order order){
        order.setUpdate_time(DateUtil.myDate());
        int isOK = traderService.updateOneOrder(order);
        if(isOK > 0){
            return Result.success(isOK);
        }
        return Result.error(CodeMsg.UPDATE_ORDER_ERROR);
    }


    //查询订单: 返回订单状态，电商id
    @GetMapping("myOrder")
    public Result<List<Order>> selectMyOrder(String traderId){
        List<Order> list = traderService.selectMyOrder(traderId);
        if(list != null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.SELECT_ORDER_ERROR);
    }

    //查询工人：根据工人id
    @GetMapping("getWorker")
    public Result<User> selectWorker(String workerId){
        User user = traderService.selectWorker(workerId);
        if(user!=null){
            return Result.success(user);
        }
        return Result.error(CodeMsg.SELECT_WORKER_ERROR);
    }


    //todo 未测试 租用仓库
    @PutMapping("rentOneHouse")
    public Result<Integer> addOneWarehouse(Rent rent){
        rent.setId(UUIDUtil.uuid());
        rent.setCreate_time(DateUtil.myDate());
        rent.setUpdate_time(DateUtil.myDate());
        rent.setStatus((long)1);
        int isOk = traderService.addOneWarehouse(rent);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.HOUSE_RENT_ERROR);
    }

    //todo 未测试 更新仓库信息包括退租
    @PutMapping("updateRent")
    public Result<Integer> updateOneRent(Rent rent){
        rent.setUpdate_time(DateUtil.myDate());
        int isOk = ownerService.updateOneRent(rent);
        if(isOk > 0){
            return Result.success(isOk);
        }
        return Result.error(CodeMsg.HOUSE_RENT_ERROR);
    }

    //todo 我的仓库
    @GetMapping("myrent")
    public Result<List<Rent>> selectMyHouse(String traderId){
        List<Rent> list = traderService.selectMyHouse(traderId);
        if(list != null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.SELECT_HOUSE_ERROR);

    }

    //所有仓库
    @GetMapping("allWarehouse")
    public Result<List<Warehouse>> selectAllHouse(){
        List<Warehouse> list = traderService.selectAllHouse();
        if(list != null){
            return Result.success(list);
        }
        return Result.error(CodeMsg.SELECT_HOUSE_ERROR);
    }
}
